/*
 *  Databaser.h
 *  openFrameworks
 *
 *  Created by fregment on 24.05.09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include <vector>
#include <sqlite3.h>
#include "ofMain.h"
#include "VanishingPoint.h"
#include "ImageSegment.h"

#include "KlausImage.h"


#ifndef ABLIEFERN_DATABASE
#define ABLIEFERN_DATABASE





namespace abliefern {
	

	
	class Databaser {
	public:
		
		enum ImageStateEnum {PICTURE_STATE_UNDEFINED = -1, PICTURE_STATE_VANISHING_POINTED = 1, PICTURE_STATE_SEGMENTING = 10, PICTURE_STATE_SEGMENTED = 20};

		
		//-------------- database handling
		
		void openDB();
		void closeDB();
		
		// ---------------write
		
		int saveImage(KlausImage* theImage);
		void saveVanishingPoint(KlausImage* theImage, VanishingPoint* theVanishingPoint);
		void saveImageSegment(ImageSegment* theSegment,VanishingPoint* theVanishingPoint);
		void setImageState(KlausImage* theImage, int theState);
		
		
		void saveKeywords(std::vector<std::string> theKeywords, int theProjectID );
		
		// --------------- read
		
		KlausImage* getImageWithState(ImageStateEnum theState);

		KlausImage* getImageFromId(int theId);
		
		KlausImage* searchPictureBySize(int theWidth, int theHeight, int theAberration);    // will later give back images of type std::vector<KlausImage>* 
		
		int returnIDFromPicture(std::string theFilename);
		
		int returnIDFromVanishingPoint(int theImageID,float theX,float theY);
		
		int returnImageIDfromVanishingPoint(int theVanishingPointID);
		
		std::vector<ImageSegment>* getSegmentsOfVanishingPoint (VanishingPoint* thePoint);
		std::vector<ImageSegment>* getSegmentsOfImage (KlausImage* theImage);
		std::vector<KlausImage> retrieveImagesWithState(ImageStateEnum theState);
		
		std::vector<VanishingPoint>* getVanishingPointsOfImage (KlausImage* theImage);
		
		std::vector<ImageSegment>* retrieveAllSegments(float theBorderPercentageMin,float theBorderPercentageMax, std::vector<std::string>* theKeywords);

		std::vector<ImageSegment>* retrieveGoodSegements(float theX, float theY, float theTolerance, float theMinArea, float theMaxArea,float theBorderPercentageMin,float theBorderPercentageMax, std::vector<int>* theIgnores, std::vector<std::string>* theKeywords);
		
		
		std::vector<int> retrieveProjectIDsWithKeywords(std::vector<std::string>* theKeywords);
		
		std::vector<std::string> retrieveKeywordsForInterface();
		

		
		
		int requestDB(std::string theRequest);
		
		
		//-------------delete
		void deleteImage(KlausImage* theImage);
		
		void deleteAllVanishingPoints(KlausImage* theImage);
		void deleteAllImageSegments(KlausImage* theImage);
		
		
		
	private:
		
		
		sqlite3 *_myDB;
		char *zErrMsg;
		char **result;
		int myRC;
		int myNrow,myNcol;
		
		
		
		std::vector<std::string> _myVcol_head;
		std::vector<std::string> _myVdata;
		
		
		int toInteger(std::string* theString);
		int toFloat(std::string* theString);
		
		
		
	};
	
}

#endif
